package com.junt.library.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.CompoundButton;

import com.junt.library.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static com.junt.library.utils.Utils.dp2px;

public class DropDownView extends CompoundButton implements View.OnClickListener {
    private Drawable drawableAction;
    private int textColor;
    private Paint paint;
    private int backColor;
    private int radius;
    private RectF rectFBack;
    private Rect rectFAction;
    private String text;
    private int textStart;
    private int textBottom;
    private boolean isChecked = false;

    public DropDownView(Context context) {
        this(context, null);
    }

    public DropDownView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DropDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackground(null);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DropDownView);
        drawableAction = typedArray.getDrawable(R.styleable.DropDownView_drop_action);
        text = typedArray.getString(R.styleable.DropDownView_drop_text);
        if (text==null){
            text="        ";
        }
        textColor = typedArray.getColor(R.styleable.DropDownView_drop_text_color, Color.WHITE);
        float textSize = typedArray.getDimensionPixelSize(R.styleable.DropDownView_drop_text_size, (int) dp2px(context,18f));
        backColor = typedArray.getColor(R.styleable.DropDownView_drop_color, Color.LTGRAY);
        radius = typedArray.getDimensionPixelSize(R.styleable.DropDownView_drop_radius, (int) dp2px(context,9f));
        typedArray.recycle();

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(textSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectFBack = new RectF(0, 0, w, h);

        rectFAction = new Rect(
                w - getPaddingRight() - drawableAction.getIntrinsicWidth(),
                h / 2 - drawableAction.getIntrinsicHeight() / 2,
                w - getPaddingRight(),
                h / 2 + drawableAction.getIntrinsicHeight() / 2);

        int space = w - getPaddingLeft() - getPaddingRight() - drawableAction.getIntrinsicWidth();
        textStart = (int) (getPaddingLeft() + (space - paint.measureText(text)) / 2);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float dis = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        textBottom = (int) (h / 2 + dis);
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        final Drawable buttonDrawable = drawableAction;
        if (buttonDrawable != null && buttonDrawable.isStateful()
                && buttonDrawable.setState(getDrawableState())) {
            invalidateDrawable(buttonDrawable);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(backColor);
        canvas.drawRoundRect(rectFBack, radius, radius, paint);

        drawableAction.setBounds(rectFAction);
        drawableAction.draw(canvas);

        paint.setColor(textColor);
        canvas.drawText(text, textStart, textBottom, paint);
    }

    @Override
    public void onClick(View v) {
        isChecked = !isChecked;
        setChecked(isChecked);
    }

    public void setDrawableAction(Drawable drawableAction) {
        this.drawableAction = drawableAction;
        invalidate();
    }

    @Override
    public void setTextColor(int textColor) {
        this.textColor = textColor;
        invalidate();
    }

    public void setBackColor(int backColor) {
        this.backColor = backColor;
        invalidate();
    }

    public void setRadius(int radius) {
        this.radius = radius;
        invalidate();
    }

    public void setText(String text) {
        this.text = text;
        requestLayout();
        invalidate();
    }

    public void setDrop(boolean up) {
        isChecked = up;
        setChecked(isChecked);
    }

    @Override
    public String getText() {
        return text;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        float width = getPaddingLeft() + getPaddingRight() + drawableAction.getIntrinsicWidth() + paint.measureText(text) + dp2px(getContext(),20);
        setMeasuredDimension((int) width, MeasureSpec.getSize(heightMeasureSpec));
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        setOnClickListener(this);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        setOnClickListener(null);
    }
}
